//======================================================================
//    This file is part of baijie_interface
//
//    Copyright (C) 2023 Shenzhen Baijie Technology Co., Ltd.All rights reserved.
//
//    filename : user_manual.cpp
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//======================================================================

#include "user_manual.h"
#include "ui_user_manual.h"
#include <QDebug>
#include <QString>
#include <QScreen>

static int g_screenWidth;
static int g_screenHeight;
static QScreen *g_screen;

user_manual::user_manual(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::user_manual)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_StyledBackground,true);
    ui->textEdit->setReadOnly(true);
    ui->treeWidget->verticalScrollBar()->setStyleSheet("QScrollBar{width:25px;}");
    ui->textEdit->verticalScrollBar()->setStyleSheet("QScrollBar{width:25px;}");
    ui->textEdit->setWordWrapMode(QTextOption::WrapAnywhere);
    g_screen = qApp->primaryScreen();
    g_screenWidth = g_screen->size().width();
    g_screenHeight = g_screen->size().height();
    setUserManualFont();
}

user_manual::~user_manual()
{
    delete ui;
}

void user_manual::on_btn_ret_clicked()
{
    emit user_manual_back_msg();
    ui->textEdit->clear();
    ui->btn_ecpandAll->setText(tr("expand all"));
    g_btnExpandAllFlag = true;
    ui->treeWidget->collapseAll();
}

void user_manual::languageReload()
{
    ui->retranslateUi(this);
}

void user_manual::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    QString str = item->text(column);

    if(str == tr("WiFi"))
    {
        ui->textEdit->setText(tr("    You can use the the switch button in the upper right control the WiFi on and off."));
    }
    else if(str == tr("Connect WiFi"))
    {
        ui->textEdit->setText(tr("    You can click the < scan > button to dispaly a list of  connected, saved, and scanned WiFi devices, "
                                 "and the list is refreshed every 10 seconds.\n    Click the WiFi you want to connect, and a connection "
                                 "dialog box will pop up for those who have not been saved before.You can enter the password in the "
                                 "dialog box to connect.For those who have been saved before, the click will be connected and jump to "
                                 "the WiFi status interface.\n    You can click the < change passwd > button to change password and click the < remove > "
                                 "button to delect WiFi after jump to  the WiFi status interface."));
    }
    else if(str == tr("Connect Hotspot"))
    {
        ui->textEdit->setText(tr("    When you click the < hotspot > button, you can choose to establish a hotspot connection or disconnect. "
                                 "After connecting the hotspot, click < change > button to modify the hotspot."));
    }
    else if(str == tr("Bluetooth"))
    {
        ui->textEdit->setText(tr("    You can use the the switch button in the upper right control the bluetooth on and off.The bluetooth list shows connected, "
                                 "saved, and scanned bluetooth devices. The list is scanned every 10 seconds, or you can click the < scan > button to scan.\n"
                                 "    You can click the connected bluetooth to view the bluetooth information, click the saved bluetooth to connect, "
                                 "and click  scan bluetooth to pair.When viewing the connected bluetooth information, you can click the button to disconnect or delete the bluetooth."));
    }
    else if(str == tr("Ethernet"))
    {
        ui->textEdit->setText(tr("    You can use the the switch button in the upper right control the Ethernet on and off."));
    }
    else if(str == tr("IP Information"))
    {
        ui->textEdit->setText(tr("    You can use < IP info > to view the IP addresses of all network configurations, which is equivalent to 'ifconfig'."));
    }
    else if(str == tr("IP Set"))
    {
        ui->textEdit->setText(tr("   You can select <automatically obtaining an IP address> to set Ethernet as dynamic network, select <use the IP address below> and enter the IP address, subnet mask, and gateway to set the Ethernet to static. "
                                 "Select <automatically obtained DNS server address> to make the network use the automatically obtained DNS server address. "
                                 "Select <use the DNS server address below> then enter the DNS address you want to set, and finally click the OK button to set all checks."));
    }
    else if(str == tr("4G"))
    {
        ui->textEdit->setText(tr("    You can use the the switch button in the upper right control the 4G on and off.Click the < status > button to view the status "
                                 "information of the module, click the < connect > button to connect to 4G, "
                                 "and click the < disconnect > button to disconnect the 4G."));
    }
    else if(str == tr("RTC"))
    {
        ui->textEdit->setText(tr("    You can use the < sync network > button to set the system time to synchronize with the network time, "
                                 "and click the button again to unsync.\n    You can use the < systimeset > button to set the system time, "
                                 "or you can use the < RTCset > button to synchronize to the hardware time."));
    }
    else if(str == tr("Backlight"))
    {
        ui->textEdit->setText(tr("    You can adjust the backlight of the g_screen through the slider."));
    }
    else if(str == tr("Sleep Time"))
    {
        ui->textEdit->setText(tr("    You can click the < sleep > button and select the sleep time to set."));
    }
    else if(str == tr("OTG"))
    {
        ui->textEdit->setText(tr("    You can click the set button to switch between the slave and host devices."));
    }
    else if(str == tr("GPIO"))
    {
        ui->textEdit->setText(tr("    You can configure the input / output of GPIO port and pull it up / down when it is set to output mode.\n"
                                 "    You can also operate on multiple GPIO ports, but each two GPIO ports should be separated by ','. \n"
                                 "    Note: there should be no space between each GPIO air, otherwise this operation will fail."));
    }
    else if(str == tr("Serial Port"))
    {
        ui->textEdit->setText(tr("    First, make sure that the hardware connection corresponds, then Click the < set  > button to configure the serial port properties, click the serial port button switch.\n"
                                 "    Enter the content you want to send in the input box, click the < send > button to send, and the receiver will receive the information."));
    }
    else if(str == tr("Record"))
    {
        ui->textEdit->setText(tr("    You can click the < start > button to record. The next time you press this button, end the recording and save "
                                 "it in '/ data' or the file directory you choose,and you can select the default name for saving recordings or change "
                                 "the name in the input box.\n    Note: the newly created folder should not contain spaces, otherwise the new recording "
                                 "file cannot be saved."));
    }
    else if(str == tr("Play"))
    {
        ui->textEdit->setText(tr("    You can click the < play > button to switch to the playback g_screen, select the path where the recording you want "
                                 "to play is located, click the < Play > button to play, slide the volume bar to adjust the volume.\n    click the < rename > "
                                 "button to rename the recording file, and click the < delete > button to delete the recording file."));
    }
    else if(str == tr("Touch Display"))
    {
        ui->textEdit->setText(tr("    This function will recognize the touch g_screen and display the movement track on the g_screen."));
    }
    else if(str == tr("Copy"))
    {
        ui->textEdit->setText(tr("    Select the file that you want to copy in the directory, you can select multiple files. Click the < copy > button "
                                 "to copy, and then select the directory you want to paste and click the < paste > button to paste."));
    }
    else if(str == tr("Cut"))
    {
        ui->textEdit->setText(tr("    Select the file that you want to cut in the directory, you can select multiple files. Click the < cut > button to cut, "
                                 "and then select the directory you want to paste and click the < paste > button to cut."));
    }
    else if(str == tr("Delete"))
    {
        ui->textEdit->setText(tr("    Select the file that you want to delete in the directory, you can select multiple files. Click the < delete > button to delete."));
    }
    else if(str == tr("Safe Exit"))
    {
        ui->textEdit->setText(tr("    You can click the < Udisk/sdcard > jump to the path of the mounted file."
                                 "Click the < safe unplug > button to save the file safely."));
    }
    else if(str == tr("All Test"))
    {
        ui->textEdit->setText(tr("    First, check the item to be tested. For USB test, select the number of external USB (0 by default), select the serial "
                                 "port number (none of the serial ports are selected by default), and then click the < begin > button to start the test. The results are displayed on the right."));
    }
    else if(str == tr("Chinese/English"))
    {
        ui->textEdit->setText(tr("    Click < Chinese/English > button to switch between Chinese and English."));
    }
}

void user_manual::setUserManualFont()
{
    qreal realX = g_screen->physicalDotsPerInchX();
    qreal realWidth = g_screenWidth / realX * 2.54;
    QFont font;
    if(realWidth < 15)
    {
        font.setPointSize(12);
    }
    else if (realWidth < 18)
    {
        font.setPointSize(14);
    }
    else
    {
        font.setPointSize(17);
    }
    ui->treeWidget->setFont(font);
    ui->btn_ecpandAll->setFont(font);
    ui->textEdit->setFont(font);
    ui->lbl_userManual->setFont(font);
}

void user_manual::on_btn_ecpandAll_clicked()
{
    if(g_btnExpandAllFlag)
    {
        ui->treeWidget->expandAll();
        ui->btn_ecpandAll->setText(tr("collapse all"));
    }
    else
    {
        ui->treeWidget->collapseAll();
        ui->btn_ecpandAll->setText(tr("expand all"));
    }
    g_btnExpandAllFlag = !g_btnExpandAllFlag;
}

void user_manual::firstDisplay()
{
    on_btn_ecpandAll_clicked();
    QTreeWidgetItemIterator it(ui->treeWidget);
    while (*it)
    {
      if ((*it)->text(0) == tr("Connect WiFi"))
      {
        (*it)->setSelected(true);
      }
      ++it;
    }
    ui->textEdit->setText(tr("    You can click the < scan > button to dispaly a list of  connected, saved, and scanned WiFi devices, "
                             "and the list is refreshed every 10 seconds.\n    Click the WiFi you want to connect, and a connection "
                             "dialog box will pop up for those who have not been saved before.You can enter the password in the "
                             "dialog box to connect.For those who have been saved before, the click will be connected and jump to "
                             "the WiFi status interface.\n    You can click the < change passwd > button to change password and click the < remove > "
                             "button to delect WiFi after jump to  the WiFi status interface."));
}

void user_manual::showEvent(QShowEvent *event)
{
    firstDisplay();
}
